/**
 * 
 */
package com.homesoft.runtime.metadata.modle.entity;

import com.homesoft.runtime.metadata.modle.AbstractRootMetadataObject;
import com.homesoft.runtime.metadata.modle.MetadataList;
import com.homesoft.runtime.metadata.modle.RootObjectType;

/**
 * @author lihg
 * 
 */
public class EntityInfo extends AbstractRootMetadataObject implements IEntityInfo {
	// 实体的类型唯一编码
	private String typeCode;
	// 实体的逻辑键信息
	private EntityKeyInfo entityKey;
	// 实体具有的属性
	private MetadataList<IPropertyInfo> properties;

	private IPropertyInfo[] allProperties;

	public EntityInfo() {
		properties = new MetadataList<IPropertyInfo>(this, "properties");
	}

	@Override
	public EntityKeyInfo getEntityKey() {
		return this.entityKey;
	}

	@Override
	public void setEntityKey(IEntityKeyInfo entityKey) {
		this.entityKey = (EntityKeyInfo) entityKey;
	}

	@Override
	public String getTypeCode() {
		return this.typeCode;
	}

	@Override
	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}

	@Override
	public MetadataList<IPropertyInfo> getProperties() {
		return this.properties;
	}

	@Override
	public IPropertyInfo get(String propertyName) {
		if (properties.contains(propertyName)) {
			return properties.get(propertyName);
		}
		return null;
	}

	@Override
	public IPropertyInfo[] GetAllProperties() {
		IPropertyInfo[] temp = new IPropertyInfo[properties.size()];
		return properties.toArray(temp);
	}

	@Override
	public IPropertyInfo FindByExtendMap(String key, String value) {
		IPropertyInfo propertyInfo = null;
		for (IPropertyInfo propInfo : this.properties) {
			boolean found = false;
			if (value == null) {
				found = propInfo.getExtData().HasExtendData(key);
			} else {
				found = propInfo.getExtData().HasExtendData(key, value);
			}
			if (found) {
				propertyInfo = propInfo;
				break;
			}
		}
		return propertyInfo;
	}

	@Override
	public IPropertyInfo FindByExtendMap(String key) {
		return this.FindByExtendMap(key, null);
	}

	@Override
	public boolean IsAssignableFrom(IEntityInfo entity) {
		return this.equals(entity);
	}

	@Override
	public RootObjectType getObjectType() {
		return RootObjectType.Entity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;

		if ((obj == this)) // 同一个引用
		{
			return true;
		}

		if (obj instanceof IEntityInfo) {
			IEntityInfo entityInfo = (IEntityInfo) obj;

			if (entityInfo.getClass() == this.getClass()) // 元数据类型不一致：
															// 一种是EntityInfo
															// 另一种是PersistentEntityInfo
			{
				return false;
			}
			return this.getFullName() == ((IEntityInfo) entityInfo).getFullName();
		}
		return false;
	}
}
